package com.wfyn.reggie.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wfyn.reggie.common.context.BaseContext;
import com.wfyn.reggie.common.web.base.BaseController;
import com.wfyn.reggie.common.web.domain.Result;
import com.wfyn.reggie.modules.sys.domain.AddressBook;
import com.wfyn.reggie.modules.sys.service.AddressBookService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * 地址簿
 * @author wfyn
 * @date 2023/10/1
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController extends BaseController {

    @Autowired
    AddressBookService addressBookService;

    /**
     * 新增地址簿
     */
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook)
    {

        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);

        return success();
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook)
    {


        return success(addressBookService.save(addressBook));

    }
    /**
     * 更新收货地址
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook)
    {


        addressBookService.update(   new LambdaUpdateWrapper<AddressBook>()
                .eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .set(AddressBook::getIsDefault,0));

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return success(addressBook);

    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public Result  getDefault()
    {

        Optional<AddressBook> addressBookOptional = Optional.ofNullable(addressBookService.getOne(new LambdaQueryWrapper<AddressBook>()
                .eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .eq(AddressBook::getIsDefault, 1)));


        return addressBookOptional
                .map(this::success)
                .orElseGet(this::failure);

    }

    /**
     * 查询指定用户的全部地址
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public  Result list(AddressBook addressBook)

    {
        //设置当前用户id
        addressBook.setUserId(BaseContext.getCurrentId());

       return  success(
               //查询列表
               addressBookService.list(
               //条件构造
               new LambdaQueryWrapper<AddressBook>()
                       //对比条件
                .eq(addressBook.getUserId()!=null,AddressBook::getUserId,addressBook.getUserId())
                       //排序条件
                .orderByDesc(AddressBook::getUpdateTime)));
    }
    @GetMapping("{id}")
    public Result  get(@PathVariable Long id)
    {


        return  success(addressBookService.getOne(new LambdaUpdateWrapper<AddressBook>().eq(
                AddressBook::getId,id
        )));

    }


}
